<script>
import global from "@/views/global/global";
import axios from "axios";
export default {
  name: "ApplyFor",
  data() {
    return {
      realName: global.user.realName,
      cars:[],
      carManage:{
        vehicleId:'',
        userId: global.user.id,
        travelers:'',
        startTime:'',
        endTime:'',
        destination:'',
        miles:'',
        approver:global.user.roleId,
        reason:''
      }
    }
  },
  methods: {
    //获取所有车辆id
    getAllCar(){
      axios.get("/oa/cars/getAllCars").then((res)=>{
        this.cars = res.data.data;
      })
    },
    cancel(){
      // 返回到我的桌面
      this.$router.push('/HomePageView')
    },
    saveCarManage(){
      if(this.carManage.vehicleId === null || this.carManage.vehicleId === undefined || this.carManage.vehicleId === ""){
        this.$message({
          type: 'warning',
          message: '车辆编号不能为空!'
        });
        return;
      }
      if(this.carManage.travelers === null || this.carManage.travelers === undefined || this.carManage.travelers === ""){
        this.$message({
          type: 'warning',
          message: '没有添加随行人员!'
        });
        return;
      }
      if(this.carManage.startTime === null || this.carManage.startTime === undefined || this.carManage.startTime === ""){
        this.$message({
          type: 'warning',
          message: '选择开始时间!'
        });
        return;
      }
      if(this.carManage.endTime === null || this.carManage.endTime === undefined || this.carManage.endTime === ""){
        this.$message({
          type: 'warning',
          message: '选择结束时间!'
        });
        return;
      }
      if(this.carManage.destination === null || this.carManage.destination === undefined || this.carManage.destination === ""){
        this.$message({
          type: 'warning',
          message: '请填写目的地!'
        });
        return;
      }
      if(this.carManage.miles === null || this.carManage.miles === undefined || this.carManage.miles === ""){
        this.$message({
          type: 'warning',
          message: '请填写里程!'
        });
        return;
      }
      if(this.carManage.reason === null || this.carManage.reason === undefined || this.carManage.reason === ""){
        this.$message({
          type: 'warning',
          message: '请填写用车事由!'
        });
        return;
      }
      axios.post("/oa/cars/saveCarManage",this.carManage).then((res)=>{
        if(res.data.code === 1){
          this.$message({
            type: 'success',
            message: '提交成功!'
          });
          this.$router.push('/HomePageView')
        }else{
          this.$message({
            type: 'error',
            message: '提交失败!'
          });
        }
      })
    }

},
  created() {
    this.getAllCar();
  }
}
</script>

<template>

  <!--标头显示-->
  <div  class="titleBox">
    用车申请
  </div>
  <el-form
      label-width="auto"
      :model="carManage"
      style="max-width: 600px"
  >
    <el-form-item label="车牌号：" >
      <el-select v-model="carManage.vehicleId" placeholder="请选择车牌号" style="width: 240px">
        <el-option
            v-for="item in cars"
            :label="item.vehicleNumber"
            :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="用车人：" >
      <el-select v-model="carManage.userId" placeholder="审批人" style="width: 240px">
        <!--        默认选中当前用户-->
        <el-option
            :label="this.realName"
            :value="this.carManage.userId"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="随行人员：">
      <el-input v-model="carManage.travelers" placeholder="请输入随行人员"/>
    </el-form-item>
    <el-form-item label="开始时间：">
      <el-date-picker
          v-model="carManage.startTime"
          type="datetime"
          placeholder="选择开始时间："
      />
    </el-form-item>
    <el-form-item label="结束时间：">
      <el-date-picker
          v-model="carManage.endTime"
          type="datetime"
          placeholder="选择结束时间："
      />
    </el-form-item>
    <el-form-item label="审批人：">
      <el-select v-model="carManage.approver" placeholder="审批人" style="width: 240px">
        <!--        默认选中当前用户-->
        <el-option
            :label="this.realName"
            :value="this.carManage.approver"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="目的地：">
      <el-input v-model="carManage.destination" placeholder="请输入目的地"/>
    </el-form-item>
    <el-form-item label="里程：">
      <el-input v-model="carManage.miles" placeholder="请输入里程"/>
    </el-form-item>
    <el-form-item label="用车事由：">
      <el-input v-model="carManage.reason" placeholder="请输入用车事由"/>
    </el-form-item>
    <el-button type="primary" @click="saveCarManage">提交</el-button>
    <el-button @click="cancel">取消</el-button>
  </el-form>

</template>

<style scoped lang="less">

</style>
